﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
using LinkCard.AdminCP.Common;
using System.Data;
namespace LinkCard.AdminCP.Deal
{
    public partial class deal_new : System.Web.UI.Page
    {
        protected Int64 nDealID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }

            if (!IsPostBack)
            {
                DataTable dt;
                // Supplier Option

                dt = DataHelper.SelectTable("SELECT SupplierID, Name FROM Supplier where Status = 2 ORDER BY Name", false);
                SupplierID.DataSource = dt;
                SupplierID.DataTextField = "Name";
                SupplierID.DataValueField = "SupplierID";
                SupplierID.DataBind();
                //SupplierID.Items.Insert(0, new ListItem("N/A", "0"));

                // Category Option 

                dt = DataHelper.SelectTable("SELECT CategoryID, [Text] FROM Category where [level] = 2 and isDisplay = 1 ORDER BY [Text]", false);
                CategoryID.DataSource = dt;
                CategoryID.DataTextField = "Text";
                CategoryID.DataValueField = "CategoryID";
                CategoryID.DataBind();
                //CategoryID.Items.Insert(0, "N/A");
            }

        }

        protected void btnPublish_Click(object sender, EventArgs e)
        {
            addDeal(1);
            addDealTemp();
            Response.Redirect("/message_box.aspx?why=1&msg=3");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            addDeal(0);
            Response.Redirect("/message_box.aspx?why=1&msg=3");
        }

        protected void addDeal(byte status)
        {
            var dao = new DealDao();
            var _deal = new LinkCard.Model.Deal();

            _deal.CategoryID = ConvertUtility.ToInt64(CategoryID.SelectedValue);
            _deal.SupplierID = ConvertUtility.ToInt64(SupplierID.SelectedValue);
            _deal.Name = Name.Value;
            _deal.Description = Description.Value;
            _deal.Detail = Detail.Value;
            _deal.Image = DealImage.Value;
            _deal.Price = ConvertUtility.ToDecimal(Price.Value);
            _deal.Discounting = ConvertUtility.ToDouble(Discounting.Value);
            _deal.TopLinkRate = ConvertUtility.ToDouble(TopLinkRate.Value);
            _deal.Point = ConvertUtility.ToInt32(Point.Value);
            _deal.EndDate = ConvertUtility.ToDateTime(EndDate.Value);
            _deal.PostDate = ConvertUtility.ToDateTime(PostDate.Value);
            _deal.Version = "1";
            _deal.Status = status;
            _deal.Tags = txtTags.Value;
            dao.Add(_deal);
            nDealID = _deal.DealID;

            if (!string.IsNullOrEmpty(_deal.Tags))
            {
                var addTag = new TagDao().AddDealTag(nDealID, _deal.Tags);
            }
        }

        protected void addDealTemp()
        {
            var dao = new DealTempDao();
            var _deal = new LinkCard.Model.DealTemp();

            _deal.CategoryID = ConvertUtility.ToInt64(CategoryID.SelectedValue);
            _deal.SupplierID = ConvertUtility.ToInt64(SupplierID.SelectedValue);
            _deal.Name = Name.Value;
            _deal.Description = Description.Value;
            _deal.Detail = Detail.Value;
            _deal.Image = DealImage.Value;
            _deal.Tags = txtTags.Value;
            _deal.Price = ConvertUtility.ToDecimal(Price.Value);
            _deal.Discounting = ConvertUtility.ToDouble(Discounting.Value);
            _deal.TopLinkRate = ConvertUtility.ToDouble(TopLinkRate.Value);
            _deal.Point = ConvertUtility.ToInt32(Point.Value);
            _deal.EndDate = ConvertUtility.ToDateTime(EndDate.Value);
            _deal.PostDate = ConvertUtility.ToDateTime(PostDate.Value);
            _deal.Version = "1";
            _deal.Status = 1;
            _deal.DealID = nDealID;
            _deal.DateLog = DateTime.Now;
            _deal.UserID = getUserID();
            
            dao.Add(_deal);
        }

        protected Int64 getUserID()
        {

            DataTable dt = DataHelper.SelectTable("SELECT UserID FROM [User] WHERE Username ='" + Session["USER"].ToString() + "'", false);
            if (dt.Rows.Count > 0)
            {
                return ConvertUtility.ToInt64(dt.Rows[0]["UserID"]);
            }
            else
            {
                return 0;
            }
        }
    }
}
